﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Drawing;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.Template;
using HF.WorkFlow.Engine;
using HF.Framework.Web;
using HF.Framework.SqlDataContract;
using HF.BaseWebCtrlLib;

public partial class BasePages_CompletedTask : System.Web.UI.Page
{
    string UserId = "";
     
    protected void Page_Load(object sender, EventArgs e)
    {
        BaseUserPage.LoadWaitPage();
        UserId = this.User.Identity.Name;
        if (!this.IsPostBack)
        InitData();
    }
    public void BuildNoRecords(GridView gridView, DataTable dt)
    {
        try
        {
            if (dt.Rows.Count == 0)
            {
                dt.Rows.Add(dt.NewRow());
                gridView.DataSource = dt;
                gridView.DataBind();
                int columnCount = gridView.Rows[0].Cells.Count;
                gridView.Rows[0].Cells.Clear();
                gridView.Rows[0].Cells.Add(new TableCell());
                gridView.Rows[0].Cells[0].ColumnSpan = columnCount;
                gridView.Rows[0].Cells[0].Text = "没有数据.";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void InitData()
    {
        DataTable dt = WorkFlowInstance.GetInstance().GetCompletedWorkTasks(UserId,1000);
        gvCompletedTask.DataSource = dt;
        gvCompletedTask.DataBind();
        BuildNoRecords(gvCompletedTask,dt);
    }
    protected void btnWorkflowMap_Click(object sender, EventArgs e)
    {
        GridViewRow dr = gvCompletedTask.SelectedRow;
        if (dr != null)
        {
            this.Response.Redirect("WorkflowMap.aspx?workflowId=" + dr.Cells[4].Text + "&workflowInstanceId=" + dr.Cells[6].Text);

        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string sqlstr = "";
        DataTable dt;

        if (cbxWorkflowCaption.Checked == false && cbxDatetime.Checked == false)
        {
            WorkDialog.ShowMessage(this, "请先选择查询条件!");
            return;
        }
        SqlDataItem sqlItem = new SqlDataItem();
        sqlstr = "  select  * from HF_WorkTaskInstanceView  WHERE UserId='" + UserId + "' and ";
        if (cbxWorkflowCaption.Checked == true)
        {
            sqlstr = sqlstr + " FlowInstanceCaption like @FlowInsCaption and ";
            sqlItem.AppendParameter("@FlowInsCaption", "%" + tbxFlowInsCaption.Text + "%");
        }
        else
        {
            sqlstr = sqlstr + " 1 =@FlowInsCaption and ";
            sqlItem.AppendParameter("@FlowInsCaption", "1");
        }
        if (cbxDatetime.Checked == true)
        {
            sqlstr = sqlstr + " taskStartTime >= @taskStartTime and ";
            sqlstr = sqlstr + " taskStartTime <= @taskEndTime  and ";
            sqlItem.AppendParameter("@taskStartTime", YYSimpleCalendar1.Text);
            sqlItem.AppendParameter("@taskEndTime", YYSimpleCalendar2.Text);
        }
        else
        {
            sqlstr = sqlstr + " 1 = @taskStartTime and ";
            sqlstr = sqlstr + " 1 = @taskEndTime and ";
            sqlItem.AppendParameter("@taskStartTime", "1");
            sqlItem.AppendParameter("@taskEndTime", "1");
        }

        sqlstr = sqlstr + "(Status='3')  order by Priority, taskStartTime desc ";
        sqlItem.CommandText = sqlstr;
        ClientDBAgent agent = new ClientDBAgent();
        dt = agent.ExecuteDataTable(sqlItem) ;
        gvCompletedTask.DataSource = dt;
        gvCompletedTask.DataBind();
        BuildNoRecords(gvCompletedTask, dt);
    }
    protected void gvCompletedTask_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        BaseUserPage.TaskGvPriorityBound(e,5);
      
    }
}
